// pages/login/login.js
import ddt from '../../api/axios.js'
const APP = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    option1: [{
        text: '全部商品',
        value: 0
      },
      {
        text: '新款商品',
        value: 1
      },
      {
        text: '活动商品',
        value: 2
      },
    ],
    option2: [{
        text: '默认排序',
        value: 'a'
      },
      {
        text: '好评排序',
        value: 'b'
      },
      {
        text: '销量排序',
        value: 'c'
      },
    ],
    value1: 0,
    value2: 'a',
    swiperH: '', //swiper高度
    nowIdx: 0, //当前swiper索引
    imgList: [ //图片列表
      "/static/swiper.png",
      "/static/swiper.png",
      "/static/swiper.png",
    ],
    msgList: [{
      title: '1'
    }, {
      title: '2'
    }],
    typelist: [{
      img: '../../static/imgs/waimai.png',
      name: '下单',
      url: '../welfare/index'
    }, {
      img: '../../static/imgs/weixin.png',
      name: '自提',
      url: '1'
    }, {
      img: '../../static/imgs/jifen.png',
      name: '消费积分',
      url: '../userinfo/integralmall/index'
    }, {
      img: '../../static/imgs/vip.png',
      name: '申请会员',
      url: '../vip/index'
    }],
    typeone: [{
      name: '精品外卖'
    }, {
      name: '精品外卖'
    }, {
      name: '精品外卖'
    }, {
      name: '精品外卖'
    }],
    shoplist: [],
    curr: 0,
    address: '',
    addresslist: '',
    userinfo:'',
    page:1,
  },
  info(){
    ddt('get','/api/personal').then(res=>{
      console.log(res)
         this.setData({
          userinfo:res.data.data.user_info
         })
    })
  },
  addressinfo() {
    ddt('get', '/api/user/addresslist').then(res => {
      this.setData({
        addresslist: res.data.data,
        address: res.data.data[0].address
      })
    })
  },
  addInfo(){
    ddt('get', '/api/indexshops?page=' + 1).then(res => {
      this.setData({
        shoplist: res.data.data
      })
      wx.setStorageSync('imgurl', res.data.data.url)
      console.log(res)
    })
  },
  shoplist() {
    ddt('get', '/api/indexshops?page=' + 1).then(res => {
      this.setData({
        shoplist: res.data.data
      })
      wx.setStorageSync('imgurl', res.data.data.url)
      console.log(res)
    })
  },
  typegopath(u) {
    
    var url = u.currentTarget.dataset.url
    // if(url == '../welfare/index'){
    //   wx.switchTab({
    //     url:'../welfare/index' ,
    //   })
    // }else 
    if (url == 1) {
      wx.showModal({
        title: "提示",
        content: '待接自提柜',
        showCancel: false,
      })
      
    }else if(url=='../vip/index'&&this.data.userinfo.membernum!=''&&this.data.userinfo.membernum!=null){
      wx.showToast({
        title: '您已经是会员',
        icon:'none'
      })
    } else {
      if (wx.getStorageSync('userinfo') == ''&&url!='../welfare/index' ){
        wx.showModal({
          title: '提示',
          content: '是否前往登陆',
          success (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../login/login',
              })
            }
          }
        })
      }else{
        wx.navigateTo({
          url,
        })
      }
     
    }

  },
  gopath() {
    if (wx.getStorageSync('userinfo') == '') {
      wx.showModal({
        title: '提示',
        content: '是否前往登陆',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../login/login',
            })
          }
        }
      })
      return
    }
    wx.navigateTo({
      url: '../address/index?id=' + 1,
    })
  },
  change(e) {
    this.setData({
      curr: e.currentTarget.dataset.index
    })
  },
  //获取swiper高度
  getHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width;
    var sH = winWid * imgh / imgw + 10 + "px"
    this.setData({
      swiperH: sH //设置高度
    })
  },
  //swiper滑动事件
  swiperChange: function (e) {
    this.setData({
      nowIdx: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if (wx.getStorageSync('userinfo') == '') {
    //   wx.navigateTo({
    //     url: '../login/login',
    //   })
    // } else {
    //   APP.logintype().then(res => {
    //     console.log(res)
    //     if (res == 2) {
    //       wx.showModal({
    //         title: '提示',
    //         content: '请前往登陆!',
    //         showCancel: false,
    //         success(res) {
    //           if (res.confirm) {
    //             wx.navigateTo({
    //               url: '../login/login',
    //             })
    //           }
    //         },
    //       })
    //     }
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    
    }
  },
swperlist(){
  ddt('post','/api/firstpage').then(res=>{
      this.setData({
        imgList:res.data.data.swiper,
        msgList:res.data.data.notice
      })
  })
},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.info()
    this.shoplist()
    this.swperlist()
    if (this.data.param == undefined) {
      this.addressinfo()
    } else {
      this.setData({
        address: this.data.param
      })
    }
  
  },
  onTabItemTap(item){
    console.log(item)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page:this.data.page*1+1
    })
    ddt('get', '/api/indexshops?page=' + this.data.page).then(res => {
      if(res.data.data!=''){
        var list  = this.data.shoplist.concat(res.data.data) 
        this.setData({
          shoplist:list
        })
      }
      
      wx.setStorageSync('imgurl', res.data.data.url)
      console.log(res)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})